﻿using System.Text.RegularExpressions;
using System.Web;

namespace music.core
{
	public static class Utils
	{
		private const string Pattern = @"(https?|ftp|file)\://[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\*\-\._~%]*)*";

		private const string Replacement = @"<a href=""$0"" target=""_blank"">$0</a>";
		private readonly static Regex regex = new Regex(Pattern, RegexOptions.IgnoreCase);

		public static string PrepareText(string text)
		{
			var str = HttpUtility.HtmlEncode(text ?? string.Empty);
			return regex.Replace(str, Replacement).Replace("\r\n", "<br />");
		}
	}
}
